<template>
    <div class="login_box">
        <h3 align="center">ECB后台管理系统</h3>
        <el-form class="login_form" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
            <el-form-item prop="username">
                <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" type="password" placeholder="请输入用户密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="login">登录</el-button>
                <el-button type="info" @click="resetLoginForm">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { reactive, toRefs, ref } from 'vue';
import axios from 'axios';
import router from '../../router';
import{ ElMessage } from 'element-plus';

// 重置按钮
function useResetLoginForm(loginFormRef) {
    const resetLoginForm = () => {
        loginFormRef.value.resetFields();
    }
    return {
        resetLoginForm,
    }
}

// 登录按钮
function useLogin(loginFormRef, state) {
    const login = () => {
        loginFormRef.value.validate(async (valid) => {
            if (!valid) return;
            const params = {
                username: state.loginForm.username,
                password: state.loginForm.password,
            };

            const res = await axios({
                url: '/adminLogin',
                method: 'post',
                params: params
            });

            if (res.data.code == 200) {
                ElMessage.success('登录成功');
                window.sessionStorage.setItem('tokenA','249972068');
                router.push('/admin');
            } else {
                ElMessage.error('登录失败')
            }
        });
    }
    return {
        login,
    }
}

export default {
    setup() {
        const loginFormRef = ref();
        const state = reactive({
            loginForm: {
                username: '',
                password: '',
            },
        });
        const loginFormRules = {
            username: [
                {
                    required: true,
                    message: '请输入用户名',
                    trigger: 'blur',
                },
                {
                    min: 3,
                    max: 10,
                    message: '用户名长度在3到10个字符之间',
                    trigger: 'blur',
                }
            ],
            password: [
                {
                    required: true,
                    message: '请输入用户密码',
                    trigger: 'blur',
                },
                {
                    min: 3,
                    max: 15,
                    message: '用户密码长度在3到15个字符之间',
                    trigger: 'blur',
                }
            ]
        }
        return {
            ...toRefs(state),
            loginFormRules,
            loginFormRef,
            ...useResetLoginForm(loginFormRef),
            ...useLogin(loginFormRef, state),
        };
    },
};
</script>

<style>
.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    margin: 0 auto;
    margin-top: 100px;
}
.login_box h3{
    margin-bottom: 20px;
}
.login_form {
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>